﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Office = Microsoft.Office.Core;

using OfficeSVN;
using Microsoft.Office.Core;


namespace PowerPointSVN {

    public partial class ThisAddIn: IOfficePlugIn {

        public static PowerPoint.Application PowerPointApplication;
        public static OfficeSvnRibbon PowerPointRibbon;
        private static SubversionController _controller;

        private void ThisAddIn_Startup(object sender, System.EventArgs e) {

            PowerPointApplication = this.Application;
            PowerPointApplication.WindowActivate += PowerPointApplication_WindowActivate;
            PowerPointApplication.PresentationSave += PowerPointApplication_PresentationSave;
        }

        public override void BeginInit()
        {
            _controller = new SubversionController(this);

            base.BeginInit();
        }
        protected override IRibbonExtensibility CreateRibbonExtensibilityObject() {

            if (_controller != null)
                return _controller.GetRibbon();

            return null;
        }

        void PowerPointApplication_PresentationSave(PowerPoint.Presentation pres) {
            OnDocumentChangedEvent(pres.FullName);
        }

        void PowerPointApplication_WindowActivate(PowerPoint.Presentation pres, PowerPoint.DocumentWindow wn) {
            OnDocumentChangedEvent(pres.FullName);
        }

        public event EventHandler<DocumentEventArgs> DocumentChanged;

        public void OpenDocument(string document)
        {
            PowerPointApplication.Presentations.Open(document, Office.MsoTriState.msoFalse, Office.MsoTriState.msoFalse, Office.MsoTriState.msoTrue);
        }

        public void CloseDocument(string document)
        {
            PowerPointApplication.ActivePresentation.Close();
        }

        /// <summary>
        /// Saves the document.
        /// </summary>
        /// <remarks>
        /// For PowerPoint there is a difference between a new (unsafed) and a safed presentation.
        /// In case of an unsafed presentation show the FileSave-Dialog.
        /// </remarks>
        public void SaveDocument(string document)
        {
            if (PowerPointApplication.ActivePresentation.Saved == MsoTriState.msoFalse)
            {
                FileDialog saveDialog = Application.get_FileDialog(MsoFileDialogType.msoFileDialogSaveAs);
                saveDialog.InitialFileName = document;
                saveDialog.Show();
            }
            else
            {
                PowerPointApplication.ActivePresentation.Save();
            }
        }

        public void ReloadDocument(string document)
        {

            // This does not work properly, the readonly-flag would not be removed/set
            // PowerPointApplication.ActivePresentation.ReloadAs(Office.MsoEncoding.msoEncodingAutoDetect);

            CloseDocument(document);
            OpenDocument(document);
        }

        private void OnDocumentChangedEvent(string document)
        {
            // Copy to a temporary variable to be thread-safe.
            EventHandler<DocumentEventArgs> temp = DocumentChanged;
            if (temp != null)
                temp(null, new DocumentEventArgs(document));
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup() {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
        }

        #endregion
    }
}
